(function (root) {
    !root.app ? root.app = {} : null;

    app.manage = {
        template: "#manage",
        data() {
            return {
                self: this,
                formValidate: {},
                groupPreList: [],
                groupProdList: [],
                groupProdBakList: [],
                groupList: [],
                appInfo: '',
                deployVo: {
                    systemName: '',
                    appName: '',
                    nickname: '',
                    gitUrl: '',
                    branchName: '',
                    params: '',
                    groupList: [],
                    imgTag: ''
                },
                prodGroupView: false,
                imgView: true,
                imgViewList: '',
                imgViewListView: false,
                loading: false
            }
        },
        created() {
            this.loadData();
        },
        watch: {
            '$route'(to, from) {
                // 对路由变化作出响应...
                this.loadData();
            }
        },
        methods: {
            //加载数据
            loadData() {
                let appName = this.$route.params.appName;
                this.groupPreList = [];
                this.groupProdList = [];
                this.groupProdBakList = [];
                this.groupList = [];
                this.groupViewList = [];
                this.imgView = true;
                this.imgViewList = '';
                this.imgViewListView = false;
                CommonApi.get("/getApp", {appName: appName}, data => {
                    this.deployVo.systemName = data.systemName ? data.systemName : '';
                    this.deployVo.appName = data.appName ? data.appName : '';
                    this.deployVo.nickname = data.nickname ? data.nickname : this.deployVo.appName;
                    this.deployVo.gitUrl = data.gitUrl ? data.gitUrl : '';
                    this.deployVo.branchName = data.branchName ? data.branchName : '';
                    this.deployVo.params = data.params ? data.params : '';
                    this.deployVo.rootPom = data.rootPom ? data.rootPom : '';
                    this.deployVo.targetPath = data.targetPath ? data.targetPath : '';
                    this.deployVo.complieBulidId = data.complieBulidId ? data.complieBulidId : '';
                    this.deployVo.imageBulidId = data.imageBulidId ? data.imageBulidId : '';
                    this.groupList = data.groupList;
                    data.groupList.forEach(item => {
                        if (item.environment === 'pre') {
                            this.groupPreList.push({
                                value: item.groupName,
                                label: item.nickname,
                                uuid: item.uuid,
                                dockerNum: item.dockerNum
                            })
                        } else {
                            this.groupProdBakList.push({
                                value: item.groupName,
                                label: item.nickname,
                                uuid: item.uuid,
                                dockerNum: item.dockerNum
                            })
                        }
                    })
                }, error => {
                    this.$Message.error(error.errors[0].msg);
                })
            },
            handleSubmit() {
                this.deployVo.groupList = this.groupViewList.map(item => {
                    let newGroup = {};
                    this.groupList.forEach(group => {
                        if (item === group.groupName) {
                            newGroup.groupName = group.groupName;
                            newGroup.environment = group.environment;
                            newGroup.uuid = group.uuid;
                            newGroup.dockerNum = group.dockerNum;
                            newGroup.nickname = group.nickname;
                        }
                    });
                    return newGroup;
                }).filter(item => item.groupName);
                var url = "/deploy";
                if (this.deployVo.imgTag) {
                    url = "/deployWithImg";
                }
                this.loading = true;
                CommonApi.post(url, this.deployVo, data => {
                    this.loading = false;
                    this.$Message.success("提交成功");
                    this.$router.push({name: 'recent'});
                }, error => {
                    this.$Message.error(error.errors[0].msg);
                })
            },

            prodGroupViewChange(value) {
                if (value) {
                    this.groupProdBakList.forEach(item => {
                        this.groupProdList.push(item);
                    })
                } else {
                    this.groupProdList.splice(0, this.groupProdList.length);
                }
            },

            imgViewChange(value) {
                if (value) {
                    this.imgViewListView = false;
                    this.deployVo.imgTag = '';
                } else {
                    this.loading = true;
                    this.getImgList();
                }
            },

            getImgList() {
                CommonApi.get("/getImg", {
                    systemName: this.deployVo.systemName,
                    appName: this.deployVo.appName
                }, data => {
                    this.imgViewList = data.map(function (item) {
                        return {label: item.imageTag, value: item.imageTag}
                    });
                    this.imgViewListView = true;
                    this.loading = false;
                }, error => {
                    this.$Message.error(error.errors[0].msg);
                })
            }
        }
    };
})(window);
